package xpn.platform.modules.sys.user;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;
import xpn.platform.common.persistence.AbstractBaseEntity;
import xpn.platform.modules.sys.org.SysOrg;
import xpn.platform.modules.sys.role.SysRole;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
 * 系统用户 实体
 *
 * @author duwei
 */
@Entity
@Getter
@Setter
@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler" }, ignoreUnknown = true)
public class SysUser extends AbstractBaseEntity {
	private static final long serialVersionUID = -6039695828223759207L;


	/**
	 * 警员编号
	 */
	@Column(length = 64, unique = true, nullable = false)
	@NotEmpty
	private String officerNumber;

	/**
	 * 所属组织机构
	 */
	@JoinColumn
	@ManyToOne(fetch = FetchType.LAZY)
	private SysOrg org;

	/**
	 * 所属客户经理
	 */
	@JoinColumn
	@ManyToOne(fetch = FetchType.LAZY)
	private SysUser manager;//关联经理

	/**
	 * 用户名
	 */
	@Column(length = 20, unique = true, nullable = false)
	@Length(min = 2, max = 20)
	@NotEmpty
	private String userName;

	/**
	 * 用户编码：可以重复，相同表示为兼职用户
	 */
	@Column(length = 20, unique = false, nullable = true)
	@Length(min = 1, max = 20)
	private String userCode;

	/**
	 * 密码
	 */
	@Column(length = 40)
	@Length(min = 0, max = 40)
	private String password;

	@Transient
	private Integer orgType;

	@Transient
	private String orgName;

	/**
	 * 密码：JSON转换时不能读到密码
	 *
	 * @return
	 */
	public String getPassword() {
		return "";
	}

	/**
	 * 使用此方法读取密码
	 *
	 * @return
	 */
	@JsonIgnore
	public String getRealPassword() {
		return password;
	}

	/**
	 * 姓名
	 */
	@Column(length = 20)
	@Length(min = 2, max = 20)
	@NotEmpty
	private String name;

	/**
	 * 备注
	 */
	@Column(length = 60)
	@Length(min = 0, max = 60)
	private String remark;

	/**
	 * 排序号
	 */
	private Float sortNo;

	/**
	 * 手机号
	 */
	@Column(length = 20)
	@Length(min = 0, max = 20)
	private String mobile;

	/**
	 * 是否启用
	 */
	private Boolean enable = true;

	@Column(length = 20, unique = true)
	private String staffNumber;//4A认证需要 员工编号 暂用userName代替

	@Column(length = 20, unique = true)
	private String indentifyNum;//4A认证需要 身份证号

	private String staffStatus;//4A认证需要 员工状态(A：可用，X：无效,S：锁定)

	@Column(columnDefinition="MEDIUMTEXT")
	private String signImg;

	@Column(columnDefinition="MEDIUMTEXT")
	private String stampImg;

	public String getStaffStatus(){
		if(this.staffStatus!=null&&!"".equals(this.staffStatus)){
			return this.staffStatus;
		}else{
			if(this.enable!=null&&this.enable.equals(false)){
              return "X";
			}
			if(this.locked!=null&&this.locked){
				return "S";
			}
		}
		return "A";
	}


	public Boolean getEnable() {
		if (enable == null) {
			return true;
		}
		return enable;
	}

	public Integer getOrgType(){
		if(this.getOrg()!=null){
			return this.getOrg().getType();
		}
		return null;
	}

	public String getOrgName(){
		if(this.getOrg()!=null){
			return this.getOrg().getOrgName();
		}
		return null;
	}

//	public void setPassword(String password){
//		if(password!=null){}
//		{
//			if(Tool.validPwd(password)){
//			  this.password=Tool.MD5(password);
//			}else{
//				throw new XpnBusinessException(XpnExceptionCodeEnum.PASSWORD_FALED);
//			}
//
//		}
//	}

	/**
	 * 是否被锁定
	 */
	private Boolean locked = false;

	public Boolean getLocked() {
		if (locked == null) {
			return false;
		}
		return locked;
	}

	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "sys_user_role", joinColumns = { @JoinColumn(name = "user_id") }, inverseJoinColumns = {@JoinColumn(name = "role_id") })
	private Set<SysRole> roles = new HashSet<>();

	/**
	 * 获取关联属性 获取角色列表
	 *
	 * @return
	 */
	@JsonIgnore
	public Set<String> fetchRoleList() {
		Set<String> roleList = new HashSet<>();
		if (this.getRoles() == null) {
			return roleList;
		}
		for (SysRole role : this.getRoles()) {
			roleList.add(role.getName());
		}
		return roleList;
	}

	/**
	 * 获取所属机构的全名
	 *
	 * @return
	 */
	@JsonIgnore
	public String fetchOrgFullName() {
		if (this.getOrg() == null) {
			return "";
		}
		return this.getOrg().fetchFullName();
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) {
			return true;
		}
		if (!(o instanceof SysUser)) {
			return false;
		}

		SysUser user = (SysUser) o;

		return getId().equals(user.getId());
	}

	/**
	 * 获取所属客户经理姓名
	 *
	 * @return
	 */
	@JsonIgnore
	public String fetchManagerName() {
		if (this.manager== null) {
			return this.name;
		}
		return this.manager.getName();
	}

	/**
	 * 获取所属客户经理姓名
	 *
	 * @return
	 */
	@JsonIgnore
	public Long fetchManagerId() {
		if (this.manager== null) {
			return null;
		}
		return this.manager.getId();
	}
}
